using AdmissionQA.Application.Dtos.ScholarshipInfo;
using AdmissionQA.Application.ServiceInterfaces;
using Microsoft.AspNetCore.Mvc;

namespace AdmissionQA.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class ScholarshipInfosController(IScholarshipInfoServices scholarshipInfoServices)
    : ControllerBase
{
    private readonly IScholarshipInfoServices _scholarshipInfoServices = scholarshipInfoServices;

    [HttpGet("all")]
    public async Task<IActionResult> GetAll()
    {
        var result = await _scholarshipInfoServices.GetAllScholarshipInfosAsync();
        return Ok(result);
    }

    [HttpGet("paged/{pageIndex}/{pageSize}")]
    public async Task<IActionResult> GetPaged(int pageIndex = 0, int pageSize = 10)
    {
        var result = await _scholarshipInfoServices.GetPagedScholarshipInfosAsync(
            pageIndex,
            pageSize
        );
        return Ok(result);
    }

    [HttpGet("search")]
    public async Task<IActionResult> Search(string scholarshipName)
    {
        var result = await _scholarshipInfoServices.SearchScholarshipInfosByNameAsync(
            scholarshipName
        );
        return Ok(result);
    }

    [HttpGet("count")]
    public async Task<IActionResult> GetCount()
    {
        var result = await _scholarshipInfoServices.GetScholarshipInfoCountAsync();
        return Ok(result);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var result = await _scholarshipInfoServices.GetScholarshipInfoByIdAsync(id);
        return Ok(result);
    }

    [HttpPost("one")]
    public async Task<IActionResult> Create(ScholarshipInfoDto scholarshipInfo)
    {
        var result = await _scholarshipInfoServices.CreateScholarshipInfoAsync(scholarshipInfo);
        return Ok(result);
    }

    [HttpPost("many")]
    public async Task<IActionResult> CreateMany(ScholarshipInfosDto scholarshipInfos)
    {
        var result = await _scholarshipInfoServices.CreateScholarshipInfosAsync(scholarshipInfos);
        return Ok(result);
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, ScholarshipInfoDto scholarshipInfo)
    {
        var result = await _scholarshipInfoServices.UpdateScholarshipInfoAsync(id, scholarshipInfo);
        return Ok(result);
    }

    [HttpDelete("one/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _scholarshipInfoServices.DeleteScholarshipInfoAsync(id);
        return Ok(result);
    }

    [HttpDelete("many")]
    public async Task<IActionResult> DeleteMany(IEnumerable<Guid> ids)
    {
        var result = await _scholarshipInfoServices.DeleteScholarshipInfosAsync(ids);
        return Ok(result);
    }

    /// <summary>
    /// 语义搜索 - 基于向量相似度的奖学金信息搜索
    /// </summary>
    /// <param name="query">用户查询文本</param>
    /// <param name="topK">返回最相似的前K个结果，默认5个</param>
    /// <returns>最相关的奖学金信息结果</returns>
    [HttpGet("semantic-search")]
    public async Task<IActionResult> SemanticSearch([FromQuery] string query, [FromQuery] int topK = 5)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("查询文本不能为空");
        }
        

        var result = await _scholarshipInfoServices.SearchScholarshipInfosByEmbeddingAsync(query, topK);
        return Ok(result);
    }

    /// <summary>
    /// 智能奖学金推荐 - 返回最相关的奖学金信息
    /// </summary>
    /// <param name="query">用户查询</param>
    /// <returns>最相关的奖学金信息</returns>
    [HttpGet("smart-recommend")]
    public async Task<IActionResult> SmartRecommend([FromQuery] string query)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("查询文本不能为空");
        }
        
        var result = await _scholarshipInfoServices.SearchScholarshipInfosByEmbeddingAsync(query, 1);
        if (result.Data != null && result.Data.Any())
        {
            var bestMatch = result.Data.First();
            return Ok(new
            {
                Query = query,
                ScholarshipName = bestMatch.ScholarshipName,
                Amount = bestMatch.Amount,
                Requirements = bestMatch.Requirements,
                Description = bestMatch.Description,
                Confidence = "高",
                Source = "智能奖学金推荐系统"
            });
        }
        
        return Ok(new
        {
            Query = query,
            Message = "抱歉，没有找到相关的奖学金信息。请尝试换个关键词搜索，或者联系学生处获取更多奖学金详情。",
            Confidence = "低",
            Source = "智能奖学金推荐系统"
        });
    }
}
